package com.ramos.kuaixiao.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.ramos.kuaixiao.App;
import com.ramos.kuaixiao.BuildConfig;
import com.ramos.kuaixiao.R;
import com.ramos.kuaixiao.network.DownloadUpdateFileTask;
import com.ramos.kuaixiao.network.UpdateBrandKeywordTask;
import com.ramos.kuaixiao.network.UpdateClientPriceTask;
import com.ramos.kuaixiao.network.UpdateCustomerStoreTask;
import com.ramos.kuaixiao.network.UpdateCustomerTask;
import com.ramos.kuaixiao.network.UpdateEmpBrandTask;
import com.ramos.kuaixiao.network.UpdateEmpStorehouseTask;
import com.ramos.kuaixiao.network.UpdateOrderTask;
import com.ramos.kuaixiao.network.UpdatePriceTypeTask;
import com.ramos.kuaixiao.network.UpdateProductBrandTask;
import com.ramos.kuaixiao.network.UpdateProductPriceTask;
import com.ramos.kuaixiao.network.UpdateSaleCustomerStoreTask;
import com.ramos.kuaixiao.network.UpdateSaleCustomerTask;
import com.ramos.kuaixiao.network.UpdateShopsTask;
import com.ramos.kuaixiao.network.UpdateStoreHouseTask;
import com.ramos.kuaixiao.network.UpdateStoreNumTask;
import com.ramos.kuaixiao.utils.CommonLoading;
import com.ramos.kuaixiao.utils.SharedPreference;
import com.ramos.kuaixiao.utils.ToastUtils;
import com.ramos.kuaixiao.utils.Utils;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Executors;

public class LoadingActivity extends Activity {
    private static String TAG = "LoadingActivity";
    private Context mContext;
    CommonLoading loading;

    @Override
    public void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        setContentView(R.layout.activity_loading);
        mContext = this;
        initView();
    }

    private void initView() {
        Utils.executeFixedRate(new ApkUpdateTask(), 0, 1000 * 60 * 2);
        loading = new CommonLoading(this);
        loading.setLoadText("获取数据中...");
        timer.schedule(task, 5000, 1000);
    }

    Timer timer = new Timer();
    TimerTask task = new TimerTask() {
        @Override
        public void run() {
            try {
                Log.d("loadData", "App.isStoreNumUpdating:" + App.isStoreNumUpdating + ",App.isOrderUpdating" + App.isOrderUpdating +
                        ",App.isEmpBrandUpdating:" + App.isEmpBrandUpdating + ",App.isEmpStorehouseUpdating:" + App.isEmpStorehouseUpdating
                        + ",App.isShopsUpdating:" + App.isShopsUpdating + ",App.isBrandKeywordUpdating:"
                        + App.isBrandKeywordUpdating + ",App.isPriceTypeUpdating:" + App.isPriceTypeUpdating + ",App.isProductPriceUpdating:" + App.isProductPriceUpdating
                        + ",App.isStoreHouseUpdating:" + App.isStoreHouseUpdating + ",App.isClientPriceUpdating:" + App.isClientPriceUpdating + ",App.isCustomerUpdating:"
                        + App.isCustomerUpdating + ",App.isCustomerStoreUpdating:" + App.isCustomerStoreUpdating + ",App.isSaleCustomerUpdating:" + App.isSaleCustomerUpdating +
                        ",App.isSaleCustomerStoreUpdating:" + App.isSaleCustomerStoreUpdating+",App.isProductBrandUpdating:"+App.isProductBrandUpdating);
                if (!App.isStoreNumUpdating && !App.isOrderUpdating && !App.isEmpBrandUpdating && !App.isEmpStorehouseUpdating && !App.isShopsUpdating
                        && !App.isBrandKeywordUpdating && !App.isPriceTypeUpdating && !App.isProductPriceUpdating &&
                        !App.isStoreHouseUpdating && !App.isClientPriceUpdating && !App.isCustomerUpdating && !App.isCustomerStoreUpdating && !App.isSaleCustomerUpdating
                        && !App.isSaleCustomerStoreUpdating&& !App.isProductBrandUpdating) {
                    loading.dismiss();
                    App.isOldVersion = false;
                    SharedPreference.setPreferString("isUpdate", "update");
                    startActivity(new Intent(LoadingActivity.this, MainActivity.class));
                    SharedPreference.setPreferString("isOldVersion", "1");
                    finish();
                    timer.cancel();
                    task.cancel();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };

    private class ApkUpdateTask extends TimerTask {

        private ApkUpdateTask() {

        }

        public void run() {
            if (Utils.isNetworkConnected(App.mContext)) {
                //库存更新
                if (!App.isStoreNumUpdating) {
                    UpdateStoreNumTask product1 = new UpdateStoreNumTask();
                    product1.executeOnExecutor(Executors.newCachedThreadPool());
                }
                //订单更新
                if (!App.isOrderUpdating) {
                    UpdateOrderTask product2 = new UpdateOrderTask();
                    product2.executeOnExecutor(Executors.newCachedThreadPool());
                }
                if (!App.isEmpBrandUpdating) {
                    UpdateEmpBrandTask product3 = new UpdateEmpBrandTask();
                    product3.executeOnExecutor(Executors.newCachedThreadPool());
                }
                if (!App.isEmpStorehouseUpdating) {
                    UpdateEmpStorehouseTask product4 = new UpdateEmpStorehouseTask();
                    product4.executeOnExecutor(Executors.newCachedThreadPool());
                }
                if (!App.isShopsUpdating) {
                    //商品更新
                    UpdateShopsTask product5 = new UpdateShopsTask();
                    product5.executeOnExecutor(Executors.newCachedThreadPool());
                }
                /*if (!App.isProductTypeUpdating) {
                    //商品类别更新
                    UpdateProductTypeTask product6 = new UpdateProductTypeTask();
                    product6.executeOnExecutor(Executors.newCachedThreadPool());
                }*/
                if (!App.isBrandKeywordUpdating) {
                    //商品分类更新
                    UpdateBrandKeywordTask product7 = new UpdateBrandKeywordTask();
                    product7.executeOnExecutor(Executors.newCachedThreadPool());
                }
                //应用更新检测
                if (!BuildConfig.DEBUG && App.mIsUpdate) {
                    DownloadUpdateFileTask product12 = new DownloadUpdateFileTask(LoadingActivity.this, 1);
                    product12.executeOnExecutor(Executors.newCachedThreadPool());
                }
                if (!App.isPriceTypeUpdating) {
                    //价格套更新
                    UpdatePriceTypeTask product8 = new UpdatePriceTypeTask();
                    product8.executeOnExecutor(Executors.newCachedThreadPool());
                }

                if (!App.isProductPriceUpdating) {
                    //商品价格表更新
                    UpdateProductPriceTask product9 = new UpdateProductPriceTask();
                    product9.executeOnExecutor(Executors.newCachedThreadPool());
                }

                if (!App.isStoreHouseUpdating) {
                    //仓库表更新
                    UpdateStoreHouseTask product10 = new UpdateStoreHouseTask();
                    product10.executeOnExecutor(Executors.newCachedThreadPool());
                }

                if (!App.isClientPriceUpdating) {
                    //商店与价格套关系表更新
                    UpdateClientPriceTask product11 = new UpdateClientPriceTask();
                    product11.executeOnExecutor(Executors.newCachedThreadPool());
                }

                if (!App.isCustomerUpdating) {
                    //商店更新
                    UpdateCustomerTask product12 = new UpdateCustomerTask();
                    product12.executeOnExecutor(Executors.newCachedThreadPool());
                }
                if (!App.isCustomerStoreUpdating) {
                    //门店更新
                    UpdateCustomerStoreTask product13 = new UpdateCustomerStoreTask();
                    product13.executeOnExecutor(Executors.newCachedThreadPool());
                }
                if (!App.isSaleCustomerUpdating) {
                    //商店关联更新
                    UpdateSaleCustomerTask product14 = new UpdateSaleCustomerTask();
                    product14.executeOnExecutor(Executors.newCachedThreadPool());
                }
                if (!App.isSaleCustomerStoreUpdating) {
                    //门店关联更新
                    UpdateSaleCustomerStoreTask product15 = new UpdateSaleCustomerStoreTask();
                    product15.executeOnExecutor(Executors.newCachedThreadPool());
                }
                if (!App.isProductBrandUpdating) {
                    //品牌更新
                    UpdateProductBrandTask product16 = new UpdateProductBrandTask();
                    product16.executeOnExecutor(Executors.newCachedThreadPool());
                }
            } else {
                ToastUtils.show("未检测到网络，无法获取数据");
                finish();
            }
        }
    }
}
